#### "On the Term "Environmental Refugee": Normative Assumptions and Empirical Results"
#### J. Matthew Hoye, Gustaf Hednäs, Michael Rowland, Umang Sodhi, Jaroslaw Kantorowicz

#### REPLICATION FILES ####

#### Install and load packages
#install.packages("tidyverse")
library(tidyverse)
#install.packages("broom.helpers")
library(broom.helpers)
#install.packages("sjPlot")
library(sjPlot)
#install.packages("rstatix")
library(rstatix)

#### Load RDS

data_file <- readRDS("replication_data.RDS")

#### Set variables types

data_file$support <- as.numeric(data_file$support)

data_file$treatment <- as.factor(data_file$treatment)

data_file$Country <- as.factor(data_file$Country) 

table(data_file$Country)
table(data_file$treatment)

#### Run OLS regression with interaction

results_interaction <- lm(formula = support ~ treatment * Country, 
                 data = data_file)

graph_interaction <- tidy_all_effects(results_interaction)

graph_interaction <- graph_interaction %>% 
  tidyr::separate(term, c("Treatment", "Country"), ":")

#### Run pooled OLS regression

results_pooled <- lm(formula = support ~ treatment, 
           data = data_file)

graph_pooled <- tidy_all_effects(results_pooled)

graph_pooled <- graph_pooled %>% 
  rename(Treatment = term) %>% mutate(Country = "Pooled")

#### Combining both regressions

combined_graph <- bind_rows(graph_interaction, graph_pooled)
combined_graph <- combined_graph %>% rename(Sample = Country)

combined_graph$Sample <- factor(combined_graph$Sample,
                                levels = c("UK", "NL", "Pooled"))

#### Figure 1 from the paper

ggplot(data = combined_graph) +
  theme_sjplot2() +
  geom_pointrange(mapping = aes(x = Treatment, 
                                y = estimate,
                                ymin = conf.low,
                                ymax = conf.high,
                                group = Sample,
                                color = Sample,
                                shape = Sample),
                  position = position_dodge(width = 0.5)) +
  theme(legend.position = "bottom",
        legend.margin=margin(t=-10),
        axis.title.y = element_text(vjust = 2),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(size = 0.6),
        panel.grid.minor.y = element_line(size = 0.6, 
                                          linetype = "dashed")) +
  labs(x = "", y = "Agree with urgent call for action ", color = "Sample") +
  ylim(4,6) +
  scale_color_manual(values = c("black", "maroon", "blue")) +
  scale_shape_manual(values = c(15, 4, 19, 23, 19)) 

#### Save Figure 1 (main graph) from the paper

ggsave("figures/main_graph.jpg",
       width = 6,
       height = 6,
       scale = 0.7,
       dpi = 500)

##### T-tests reported in the paper

t.test(formula = support ~ treatment, data = data_file[data_file$Country=="UK",])

t.test(formula = support ~ treatment, data = data_file[data_file$Country=="NL",])

t.test(formula = support ~ treatment, data = data_file)

##### Cohen's d

data_file %>% filter(Country=="NL") %>% cohens_d(support ~ treatment, var.equal = FALSE)

data_file %>% cohens_d(support ~ treatment, var.equal = FALSE)

